//
//  RHOrganizationBindVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/3/21.
//

#import "RHOrganizationBindVC.h"

@interface RHOrganizationBindVC ()<QMUITextFieldDelegate>
@property (nonatomic, strong) QMUITextField *orgIdTextField;

@property (nonatomic, strong) QMUITextField *orgNameTextField;

@property (nonatomic, assign) long long curOrgId;
@end

@implementation RHOrganizationBindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"绑定组织".rh_localized;
    self.view.backgroundColor = CFTabViewBgColor;
    [self setupUI];
}

-(void)setupUI
{
    
    QMUIButton *searchBtn = [[QMUIButton alloc]init];
    [searchBtn setTitle:@"搜索".rh_localized forState:UIControlStateNormal];
    [searchBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [searchBtn setCornerRadius:8];
    [searchBtn setBackgroundColor:CMainColorGreen];
    searchBtn.titleLabel.font = kSYSTEMFONT(14);
    [self.view addSubview:searchBtn];
    [searchBtn.titleLabel sizeToFit];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(searchBtn.titleLabel.width+40);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
        make.height.offset(44);
    }];
    
    _orgIdTextField = [[QMUITextField alloc]init];
    _orgIdTextField.placeholder = @"请输入组织ID".rh_localized;
    _orgIdTextField.font = kSYSTEMFONT(14);
    _orgIdTextField.textInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [_orgIdTextField rounded:8 width:1 color:kBorderColor];
    _orgIdTextField.backgroundColor = UIColor.whiteColor;
    _orgIdTextField.returnKeyType = UIReturnKeySearch;
    _orgIdTextField.delegate = self;
    [self.view addSubview:_orgIdTextField];
    [_orgIdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.height.equalTo(searchBtn);
        make.right.equalTo(searchBtn.mas_left).offset(-15);
    }];
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确认绑定".rh_localized forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [sureBtn setCornerRadius:8];
    [sureBtn setBackgroundColor:CMainColorGreen];
    sureBtn.titleLabel.font = kBOLDSYSTEMFONT(18);
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
        make.height.offset(50);
    }];
    kWeakSelf;
    _orgNameTextField = [[QMUITextField alloc]init];
    [_orgNameTextField rounded:8 width:1 color:kBorderColor];
    _orgNameTextField.font = kSYSTEMFONT(14);
    _orgNameTextField.textInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    _orgNameTextField.placeholder = @"组织名称".rh_localized;
    _orgNameTextField.backgroundColor = UIColor.whiteColor;
    _orgNameTextField.enabled = NO;
    [self.view addSubview:_orgNameTextField];
    [_orgNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(44);
        make.top.equalTo(weakSelf.orgIdTextField.mas_bottom).offset(15);
    }];
    
    [[_orgNameTextField rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
        sureBtn.enabled = x.length>0;
    }];
    
    [[searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf searchAction];
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf bindToOrganization];
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self searchAction];
    
    return  YES;
}


-(void)searchAction
{
    if (self.orgIdTextField.text.length == 0) {
        return;
    }
    [QMUITips showLoadingInView:kAppWindow];
    [BaseModel startGetWithCompletionAPIKey:H_getOrganizationDetail argument:@{@"orgId":@([self.orgIdTextField.text integerValue])} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            self.curOrgId = [request.responseJSONObject[@"id"] longLongValue];
            self.orgNameTextField.text = ValidNullData(request.responseJSONObject[@"data"][@"brandName"]);
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
    
}

-(void)bindToOrganization
{
    if (self.orgIdTextField.text.length == 0) {
        return;
    }
    [QMUITips showLoadingInView:kAppWindow];
    [BaseModel startWithCompletionAPIKey:H_bindToOrganization argument:@{@"orgId":@([self.orgIdTextField.text longLongValue])} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            [QMUITips showSucceed:@"绑定成功".rh_localized inView:self.view hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //绑定成功后获取一下个人信息,根据角色重新加载对应控制器
                [QMUITips showLoadingInView:kAppWindow];
                [[PrivateMange share]getUserInfoSuccess:^(RHUserModel * _Nonnull userModel) {
                    [[NSNotificationCenter defaultCenter]postNotificationName:UserRoleDidChangeNotification object:nil];
                }];
            });
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}


@end
